/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.util.List;
import model.pojo.Hinh;
import model.pojo.Khachhang;
import model.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author CUCAINHO
 */
public class HinhDAO {

    public static List<Hinh> DanhSachHinh(int maDeal) {

        List<Hinh> danhSachHinh = null;

        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = " from Hinh h where h.maDeal=:maDeal";

            Query query = session.createQuery(hql);
            query.setString("maDeal", Integer.toHexString(maDeal));

            danhSachHinh = query.list();
           // Insert(2,4,1,10);
            


        } catch (HibernateException ex) { //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return danhSachHinh;
    }

    public static void Insert(int maTu, int maDen, int hinhTu, int hinhDen) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        try {

            for (int k = maTu; k <= maDen; k++) {
                String strMa = Integer.toString(k);
                for (int i = hinhTu; i <= hinhDen; i++) {
                    String stri = Integer.toString(i);
                    String tenHinh = "Hinh " + stri;
                    String url = "images/deal/" + strMa + "_h" + stri + ".jpg";

                    Hinh h = new Hinh(tenHinh, url, k, 1);
                    session.beginTransaction();
                    session.save(h);
                    session.getTransaction().commit();


                }
            }



        } catch (HibernateException ex) { //Log the exception 
            System.err.println(ex);

        } finally {
            session.close();
        }



    }
}
